package com.abeframe.core.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.widget.TextView;

import java.util.HashMap;

import butterknife.ButterKnife;

public abstract class MVPBaseFragmentActivity<V, P extends MVPBasePresenter<V>> extends FragmentActivity {

    protected P mPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(getThemeResId());
        setContentView(getLayoutResId());
        //初始化数据
        ButterKnife.bind(this);
        init(savedInstanceState);
        mPresenter = createPresenter();//创建Presenter
        //填充View
        if (mPresenter != null) {
            mPresenter.attachView((V) this);
        }
        initDatas();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }

    protected abstract P createPresenter();

    public void setText(TextView tv, Object obj) {
        String value = String.valueOf(obj);
        String text = "";
        if (!TextUtils.isEmpty(value) && !value.toUpperCase().equals("NULL")) {
            text = value;

        }
        tv.setText(text);
    }

    /**
     * 获取context
     */
    public Context getThisContext() {
        return this;
    }


    /**
     * 设置主题
     */
    protected abstract int getThemeResId();

    /**
     * 返回布局资源ID
     */
    protected abstract int getLayoutResId();

    /**
     * 初始化
     */
    protected abstract void init(Bundle savedInstanceState);


    /**
     * 实现功能，填充数据
     */
    protected abstract void initDatas();


    /**
     * 处理传值
     *
     */
    public HashMap<String, Object> getFilter() {
        if (getIntent() != null && getIntent().getSerializableExtra("filter") != null) {
            HashMap<String, Object> filter = (HashMap<String, Object>) getIntent().getSerializableExtra("filter");
            return filter;
        }
        return null;
    }

    /**
     * 跳转到指定页面
     *
     * @param cls cls
     */
    public void goToActivity(Class<?> cls) {
        goToActivity(cls, null);
    }

    /**
     * 跳转到指定页面
     *
     * @param cls cls
     * @param filter filter
     */
    public void goToActivity(Class<?> cls, HashMap<String, Object> filter) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (filter != null) {
            intent.putExtra("filter", filter);
        }
        startActivity(intent);
        //  overridePendingTransition(R.anim.modal_activity_open_enter, R.anim.modal_activity_open_exit);
    }


    /**
     * 跳转到指定页面
     *
     * @param cls cls
     * @param code code
     */
    public void goToActivityForResult(Class<?> cls, int code) {
        goToActivityForResult(cls, code, null);
    }

    /**
     * 跳转到指定页面
     *
     * @param cls cls
     * @param code code
     * @param filter filter
     */
    public void goToActivityForResult(Class<?> cls, int code,
                                      HashMap<String, Object> filter) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (filter != null) {
            intent.putExtra("filter", filter);
        }
        startActivityForResult(intent, code);
    }

    /**
     * 关闭制定页面
     */
    public void CloseActivity() {
        finish();
    }

    /**
     * 关闭制定页面
     *
     * @param mode mode
     */
    public void CloseActivityForResult(HashMap<String, Object> mode) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("filter", mode);
        Intent intent = new Intent();
        intent.putExtras(bundle);
        // 返回intent
        setResult(RESULT_OK, intent);
        finish();
    }

}
